package za.co.dylanjaneke.airdownloader.command
{
	import flash.system.System;
	
	import mx.controls.Alert;
	
	import za.co.dylanjaneke.airdownloader.events.CopyAllLinksEvent;
	import za.co.dylanjaneke.airdownloader.model.StatusPM;
	import za.co.dylanjaneke.airdownloader.model.WeekViewPM;
	import za.co.dylanjaneke.airdownloader.model.vo.DayEntryVO;
	import za.co.dylanjaneke.airdownloader.model.vo.ShowVO;

	/**
	 * Author: Dylan Janeke.
	 */
	[Name("copyAllLinksCommand")]
	public class CopyAllLinksCommand
	{
		[Inject]
		public var weekViewPM:WeekViewPM;
		
		[Inject]
		public var statusPM:StatusPM;
		
		[Observer]
		public function handleCopyAllLinksEvent(event:CopyAllLinksEvent): void
		{
			var numLinks:int=0;
			var links:String="";
			for each(var day:DayEntryVO in weekViewPM.shows)
			{
				for each(var show:ShowVO in day.shows )
				{
					if(show.children.length>0)
					{
						for each(var lnk:ShowVO in show.children)
						{
							if(lnk.ticked)
							{
								links+= lnk.label + "\n";
								numLinks++;								
							}
						}
					}
				}
			}
			if(numLinks==0)
			{
				Alert.show("No links have been selected to copy","No Links Selected");
			}
			else
			{	
				statusPM.status= numLinks + " download link" + (numLinks==1?"":"s") + " copied to the clipboard.";
			}
			
			System.setClipboard(links);
		}
	}
}